package org.netty.push.api.service.impl;

import java.util.UUID;

import org.netty.push.api.dto.PushRequest;
import org.netty.push.api.exception.BussnessExcetpion;
import org.netty.push.api.exception.ErrorCode;
import org.netty.push.api.service.MessageService;
import org.netty.push.core.proto.DataRequestMsg;
import org.netty.push.server.channel.ChannelHolderUtlil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import io.netty.channel.Channel;

@Component
public class MessageServiceImpl implements MessageService {

	@Override
	public String singlePush(PushRequest pushRequest){
		if (null != pushRequest) {
			Channel channel = ChannelHolderUtlil.get(pushRequest.getDeviceToken());
	
			if(null!=channel) {
				DataRequestMsg dataRequestMsg=new DataRequestMsg();
				BeanUtils.copyProperties(pushRequest, dataRequestMsg);
				dataRequestMsg.setId(UUID.randomUUID().toString());
				channel.writeAndFlush(dataRequestMsg);
				return dataRequestMsg.getId();
			}else {
				throw new BussnessExcetpion(ErrorCode.SYS_ERROR);
			}
		}

		return null;
	}

}
